<?php
/**
 * Application autoloader
 * 
 * @author Alan Moreau - moreau.alan@gmail.com
 *
 */
class Mac_Autoloader
{
    /**
     * Load required files according to source file naming convention
     * 
     * @param string $className the class Mac_to load
     * @return boolean true if the file exists otherwise false
     */
    public static function autoload ($className)
    {
        $file = "";
        $matches = array();
        if (preg_match('/^(([^_]+)_)?(.+)(Controller|Form)$/', $className, $matches) && ($className !== 'Mac_Controller') && ($className !== 'Mac_Form')) {
            $module = ($matches[2] != '') ? strtolower($matches[2]) : 'default';
            $file = 'module'.'/'.strtolower($module.'/'.$matches[4]).'/'.str_replace('_', '/', $matches[3]).'.php';
        } else {
            $file = str_replace('_', '/', $className).'.php';
        }
        
        //mimic default autoloader process
        $includePaths = get_include_path();
        $paths = explode(PATH_SEPARATOR, $includePaths);
        
        foreach($paths as $path){
            $sourceFile = realpath($path.'/'.$file);
            if (file_exists($sourceFile)){
                require_once($sourceFile);
                return true;
            }
        }
        
        return false;
    }
}